{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "k近邻算法在测试集上准确率\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "1.0"
     },
     "metadata": {},
     "execution_count": 1
    }
   ],
   "source": [
    "from sklearn.datasets import load_breast_cancer \n",
    "\n",
    "data=load_breast_cancer()\n",
    "x=data['data']\n",
    "y=data['target']\n",
    "from sklearn.model_selection import train_test_split\n",
    "# 划分训练集集和测试集，训练集和测试集的比例是7:3\n",
    "x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=3) \n",
    "# 导入k近邻算法\n",
    "from sklearn.neighbors import KNeighborsClassifier \n",
    "\n",
    "model=KNeighborsClassifier(n_neighbors=3) #k近邻算法k参数为3           \n",
    "model.fit(x_train,y_train)                #将训练集传入模型模型进行训练\n",
    "print('k近邻算法在测试集上准确率')\n",
    "model.score(x_test,y_test)              #k近邻算法算法评估结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "svm算法在测试集上准确率\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.8771929824561403"
     },
     "metadata": {},
     "execution_count": 2
    }
   ],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "#划分数据集\n",
    "x_trainval,x_test,y_trainval,y_test=train_test_split(x,y,test_size=0.8)\n",
    "x_train,x_val,y_train,y_val=train_test_split(x_trainval,y_trainval,train_size=0.7)\n",
    "#svm算法导入\n",
    "from sklearn.svm import SVC\n",
    "best_score=0.0\n",
    "for gamma in [0.001,0.01,0.1,1,10,100]:\n",
    "     for C in [0.001,0.01,0.1,1,10,100]:\n",
    "        svm=SVC(gamma=gamma,C=C)\n",
    "        svm.fit(x_train,y_train)\n",
    "        score=svm.score(x_val,y_val)\n",
    "        if score>best_score:\n",
    "            best_score=score\n",
    "            best_parameters={'gamma':gamma,'C':C}\n",
    "\n",
    "svm=SVC(**best_parameters)\n",
    "svm.fit(x_train,y_train)\n",
    "print('svm算法在测试集上准确率')\n",
    "svm.score(x_test,y_test)#svm分类算法评估结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "逻辑回归模型在测试集上准确率\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.9210526315789473"
     },
     "metadata": {},
     "execution_count": 3
    }
   ],
   "source": [
    "from sklearn.linear_model import LogisticRegression\n",
    "LR=LogisticRegression()\n",
    "LR.fit(x_train,y_train)\n",
    "print('逻辑回归模型在测试集上准确率')\n",
    "LR.score(x_test,y_test)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "朴素贝叶斯模型在测试集上准确率\n"
    },
    {
     "output_type": "execute_result",
     "data": {
      "text/plain": "0.9364035087719298"
     },
     "metadata": {},
     "execution_count": 4
    }
   ],
   "source": [
    "#朴素贝叶斯\n",
    "from sklearn.naive_bayes import GaussianNB,BernoulliNB,MultinomialNB\n",
    "gnb=GaussianNB()\n",
    "gnb.fit(x_train,y_train)\n",
    "print('朴素贝叶斯模型在测试集上准确率')\n",
    "gnb.score(x_test,y_test)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.7-final"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}